# coding=utf-8
import random
import sys
import time

import requests
import threadpool


def test_run_task(item, index, log):
    session = requests.session()
    url_list = [
        'http://www.baidu.com',
        'http://www.163.com/',
        'http://www.sohu.com/',
        'http://www.qq.com/',
    ]
    try:
        # 防呆措施
        rand_inx = random.randint(0, len(url_list) - 1)
        if rand_inx > len(url_list) - 1:
            rand_inx = 0
        if rand_inx < 0:
            rand_inx = len(url_list) - 1

        proxy = {'http': item['http']}
        session.proxies = proxy
        resp = session.get(url_list[rand_inx], timeout=10)
        if resp.status_code == 200:
            log.info(str(session.proxies) + ':SUCCESS:' + str(index))
        else:
            log.info(str(session.proxies) + ':FAIL:' + str(index))
            item['status'] = 0
    except Exception:
        item['status'] = 0
        log.info(str(session.proxies) + ':exception:' + str(index))


class ProxyLocal(object):
    obj_count = 0
    statis_proxy = []

    def __init__(self, proxies_file='./config/proxies_200.txt', log=None):

        self.log = log

        self.proxy_list = []

        # 加载代理信息
        ProxyLocal.load_steady_proxy(proxies_file, log)

        # 加载代理信息
        self.proxy_list = [{"http": item["http"]} for item in ProxyLocal.statis_proxy if item['status'] == 1]
        log.info('加载代理个数为: {length}'.format(length=len(self.proxy_list)))

        if len(self.proxy_list) <= 100:
            self.log.error('代理数目异常!!!!!!!!!!!')

        # 随机初始位置
        self.index = random.randint(0, len(self.proxy_list) - 1) if len(self.proxy_list) >= 1 else 0

    @staticmethod
    def load_steady_proxy(proxies_file, log):
        if ProxyLocal.obj_count == 0:
            ProxyLocal.obj_count = 1
            with open(proxies_file) as fp:
                for line in fp:
                    line = line.strip()
                    proxy = {'http': 'http://' + line, 'status': 1}
                    ProxyLocal.statis_proxy.append(proxy)

            # # 增加测试定义, 防止调试等待测试代理
            if len(sys.argv) > 1:
                ProxyLocal.test_steady_proxy(ProxyLocal.statis_proxy, log)

    def get_local_proxy(self):
        length = len(self.proxy_list)
        if length <= 0:
            return None
        self.index += 1
        self.index %= length
        return self.proxy_list[self.index]

    @staticmethod
    def test_steady_proxy(proxy_list, log):
        log.info('开始进行代理测试..')
        start_time = time.time()
        pool = threadpool.ThreadPool(16)
        for index, item in enumerate(proxy_list):
            req = threadpool.WorkRequest(test_run_task, [item, index, log])
            pool.putRequest(req)
        pool.wait()
        end_time = time.time()
        log.info('代理测试完成..')
        log.info('use time: {use_time}'.format(use_time=end_time - start_time))


if __name__ == '__main__':
    proxies = ProxyLocal('../config/proxies_200.txt')
